package com.ydgk.controller;

import com.ydgk.bean.*;
import com.ydgk.service.GoodsService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/goods")
@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    private static final Integer pageSize = 8;
    private static final String news1Name = "花瓶花器";
    private static final String news2Name = "干花花艺";
    private static final String news3Name = "浓情·欧式";

    @ModelAttribute
    public void getFurnitureDetail(@RequestParam(value = "fid" , required = false , defaultValue = "0") Integer fid,Map map){
        if(fid != 0){
            Furniture furniture = goodsService.findFurnitureById(fid);
            List<View> viewList = goodsService.findViewByFid(fid);
            List<Detail> detailList = goodsService.findDetailByFid(fid);
            List<FurnitureKinds> furnitureKindsList = goodsService.findFurnitureKindsByFid(fid);
            map.put("furniture",furniture);
            map.put("viewList",viewList);
            map.put("detailList",detailList);
            map.put("furnitureKindsList",furnitureKindsList);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getAllGoods")
    public AjaxResult getAllGoods(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort,
                                  @RequestParam(value = "kid", required = false, defaultValue = "0") Integer kid,
                                  @RequestParam(value = "ktid", required = false, defaultValue = "0") Integer ktid,
                                  @RequestParam(value = "likeName" , required = false, defaultValue = "") String likeName){
        AjaxResult ajaxResult = new AjaxResult();
        try{
        Map<String, Object> factorMap = new HashMap<String, Object>();
        factorMap.put("likeName",likeName);
        factorMap.put("type","onSale");
        factorMap.put("kid",kid);
        factorMap.put("ktid",ktid);
        int count = goodsService.findAllFurnitureCount(factorMap);
        Integer pageTotal = 0;
        if(count <= pageSize){
            pageTotal = 1;
        }else{
            if (count % pageSize == 0) {
                pageTotal = count / pageSize;
            } else {
                pageTotal = count / pageSize + 1;
            }
        }
        if(pageNo > pageTotal){
            pageNo = pageTotal;
        }
        if(pageNo < 1){
            pageNo = 1;
        }

        Integer pageStart = (pageNo - 1) * pageSize;
        factorMap.put("start", pageStart);
        factorMap.put("size", pageSize);
        factorMap.put("sort", sort);

        List<Furniture> furnitureList =  goodsService.findAllFurniture(factorMap);

        String pageContent = Tool.getPageContentForPortal(pageNo,pageTotal,"getAllGoods");
        Page<Furniture> page = new Page(furnitureList, pageNo, pageTotal,count,pageContent);
        ajaxResult.setData(page);
        ajaxResult.setSuccess(true);
    }catch (Exception e){
        ajaxResult.setSuccess(false);
    }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getTopInfo")
    public AjaxResult getTopInfo(){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            List<Kinds> kindsList = goodsService.findAllKinds();
            Map<Integer , List<KindsType>> kindsTypeMap = new HashMap<Integer , List<KindsType>>();
            int kid = 0;
            for (Kinds k: kindsList) {
                kid =  k.getKid();
                kindsTypeMap.put(kid,goodsService.findAllKindsTypeByKid(kid));
            }
            TopInfo topInfo = new TopInfo(kindsList,kindsTypeMap);
            ajaxResult.setData(topInfo);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getNews")
    public AjaxResult getNews(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            Map<String, Object> factorMap = new HashMap<String, Object>();
            factorMap.put("start", 0);
            factorMap.put("size", 3);
            factorMap.put("sort", 5);
            KindsType kindsType1 = goodsService.findKindsTypeByKtname(news1Name);
            factorMap.put("ktid",kindsType1.getKtid());
            List<Furniture> news1 = goodsService.findAllFurniture(factorMap);
            KindsType kindsType2 = goodsService.findKindsTypeByKtname(news2Name);
            factorMap.put("ktid",kindsType2.getKtid());
            List<Furniture> news2 = goodsService.findAllFurniture(factorMap);
            KindsType kindsType3 = goodsService.findKindsTypeByKtname(news3Name);
            factorMap.put("ktid",kindsType3.getKtid());
            factorMap.put("size", 12);
            List<Furniture> news3 = goodsService.findAllFurniture(factorMap);
            ToPortalInfo toPortalInfo = new ToPortalInfo(news1,news2,news3);
            ajaxResult.setData(toPortalInfo);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getAllKindsType")
    public AjaxResult getAllKindsType(@RequestParam("kid")Integer kid){
        List<KindsType> kindsTypeList = goodsService.findAllKindsTypeByKid(kid);
        AjaxResult ajaxResult = new AjaxResult();
        Page page = new Page(kindsTypeList,null,null,null,null);
        ajaxResult.setData(page);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getFurnitureByKtid")
    public AjaxResult getFurnitureByKtid(@RequestParam("ktid") Integer ktid){
        Map<String, Object> factorMap = new HashMap<String, Object>();
        factorMap.put("type","onSale");
        factorMap.put("ktid",ktid);
        factorMap.put("start", 0);
        factorMap.put("size", 6);
        factorMap.put("sort", 5);
        List<Furniture> furnitureList = goodsService.findAllFurniture(factorMap);
        AjaxResult ajaxResult = new AjaxResult();
        Page page = new Page(furnitureList,null,null,null,null);
        ajaxResult.setData(page);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }


    @ResponseBody
    @RequestMapping(value = "/getFurnitureDetail")
    public AjaxResult getFurnitureDetail(Furniture furniture, List<View> viewList, List<Detail> detailList, List<FurnitureKinds> furnitureKindsList){
        AjaxResult ajaxResult = new AjaxResult();
        FurnitureDetail furnitureDetail = new FurnitureDetail(furniture,viewList,detailList,furnitureKindsList);
        ajaxResult.setData(furnitureDetail);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @RequestMapping(value = "/findGoods")
    public String findGoods(@RequestParam("likeName") String likeName,Map map){
        map.put("likeName",likeName);
        return "Portal/allGoods";
    }
}
